package com.qk.management.task;

import com.qk.management.service.UserService;
import com.qk.utils.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/10/10 20:30
 * @description
 *
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class ScheduledDeleteTask {

    @Autowired
    private AliYunOSSOperators aliYunOSSOperators;

    @Autowired
    private UserService userService;


    // 每天凌晨2点执行删除任务
    @Scheduled(cron = "0 0 2 * * ?")
    public void executeDeleteTask() {
        try {
            log.info("开始执行定时删除任务,当前时间为={}", LocalDateTime.now());

            // 这里需要获取要删除的文件列表
            List<String> filesToDelete = getFilesToDelete();

            if (!CollectionUtils.isEmpty(filesToDelete)) {
                aliYunOSSOperators.deleteFiles(filesToDelete);
            } else {
                log.info("没有需要删除的文件");
            }
        } catch (Exception e) {
            log.error("定时删除任务执行失败={}", e.getMessage());
        }
    }

    // 获取需要删除的文件列表的方法（需要根据业务需求实现）
    private List<String> getFilesToDelete() {
        List<String> files = userService.getImages();
        return files.stream().map(file -> file.substring(file.lastIndexOf("/") + 1)).toList();
    }
}
